package com.example.tanlee.ocrgraduation.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.example.tanlee.ocrgraduation.R;

/**
 * 存放自己常用的参数
 * Created by Tanlee on 2016/12/13.
 */

public class MyUtil {

    public static int[] bitmap2IntArray(Context context,int id){
        Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(), id); ;
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int[] pix = new int[w * h];
        bitmap.getPixels(pix, 0, w, 0, 0, w, h);
        return pix;
    }

    public static Bitmap intArray2Bitmap(int width,int height,int[] intArray){
        Bitmap bitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
        //faceall为返回的int数组
        bitmap.setPixels(intArray,0,width,0,0,width,height);
        return bitmap;
    }

    /**
     * 将c++中的数组转换成基本的java数组，以java的为准
     * @param sourceArray
     * @return
     */
    public static int[] cArrayConvertBitmap(int[] sourceArray){
        for(int i=0;i<sourceArray.length;i++){
            sourceArray[i] = (0xff << 24) | (sourceArray[i]<<16) | (sourceArray[i]<<8) | sourceArray[i];
        }
        return sourceArray;
    }

    /**
     * 将数组转换成为bitmap，然后将bitmap通过imageView直接显示出来
     * @param targetArray
     * @param showIv
     * @param width
     * @param height
     */
    public static void showBitmap(int[] targetArray, ImageView showIv,int width,int height){
        targetArray = cArrayConvertBitmap(targetArray);
        Bitmap result = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        result.setPixels(targetArray, 0, width, 0, 0,width, height);
        showIv.setImageBitmap(result);
    }


}
